package com.tihi.ui.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 
 * @author duantihi
 * @date 2016-1-30 下午1:11:15
 */
public abstract class BaseFragment extends Fragment{
	
	protected BaseActivity mActivity;
	protected boolean isDetach;
	private View mView;

	public BaseFragment(){
		super();
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
        isDetach = false;
    }
	
	@Override
	public void onDetach() {
		super.onDetach();
		isDetach = true;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(mView == null){
			mView = inflater.inflate(getLayoutId(), container, false);
			 initialViews(mView, inflater);
		} else{
			ViewGroup parent = (ViewGroup)mView.getParent();
			if (parent != null) {  
	            parent.removeView(mView);
	        } 
		}
		return mView;
	}

	 /**
     * 设置Layout ID
     *
     * @return
     */
    abstract public int getLayoutId();

    /**
     * 初始化界面变量
     *
     * @param root
     */
    abstract public void initialViews(View root, LayoutInflater inflater);
    

}
